package app;

import java.io.FileInputStream;
import java.io.IOException;

import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import model.*;

import view.AppView;
import controller.AppController;
import controller.ControllerException;

/**
 * Main class
 * 
 * This class initializes the basic MVC framework of the CoolMail application.
 * @author MUSAB, Buthainah
 * @since May 2013
 * @version $Revision: 1.0 $
 */
public class Init {
	
	// App configurations
	private static Properties config;
	
	// Configurations path
	private static final String configPath = System.getProperty("user.dir") + "/config/config.properties";
	
	static AppModel model;
	static AppView  view;
	static AppController controller;
	
	/**
	 * Main method
	 * @param args
	
	 * @throws ControllerException */
	public static void main(String[] args) throws ControllerException{
		
		// load system configurations
		loadConfigurations();

		// Init the controller which will then create the models and views
    	model = new AppModel(config);
		view = new AppView();
		controller = new AppController(model, view);

    			
	}

	/**
	 * Loads system configurations in the config variable
	 */
	private static void loadConfigurations() {

		config = new Properties();
    	try {
    		config.load(new FileInputStream(configPath));
    	} catch (IOException ex) {
    		ex.printStackTrace();
    		JOptionPane.showMessageDialog(
    				new JFrame(),
    				"Configurations could not be loaded");
    		System.exit(0);
        }
	}
}
